<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Bank extends CI_Controller {

/*
| -----------------------------------------------------
| PRODUCT NAME: 	STOCK MANAGER ADVANCE 
| -----------------------------------------------------
| AUTHER:			MIAN SALEEM 
| -----------------------------------------------------
| EMAIL:			saleem@tecdiary.com 
| -----------------------------------------------------
| COPYRIGHTS:		RESERVED BY TECDIARY IT SOLUTIONS
| -----------------------------------------------------
| WEBSITE:			http://tecdiary.net
| -----------------------------------------------------
|
| MODULE: 			Customers
| -----------------------------------------------------
| This is customers module controller file.
| -----------------------------------------------------
*/

	 
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}
		
		$this->load->library('form_validation');
		$this->load->model('demo/bank_model');


	}
	
   function get_all_bank()
   {
	  /* $groups = array('purchaser', 'viewer');
		if ($this->ion_auth->in_group($groups))
		{
			$this->session->set_flashdata('message', $this->lang->line("access_denied"));
			$data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
			redirect('home', 'refresh');
		}*/
	  $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
	  
	  /*
	  $this->load->library("pagination");
	  
	    $config = array();
        $config["base_url"] = base_url() . "customers/index";
        $config["total_rows"] = $this->customers_model->customers_count();
        $config["per_page"] = ROWS_PER_PAGE;
		$config['num_links'] = 4;
        $config["uri_segment"] = 3;
		//$config['use_page_numbers'] = TRUE;
		$config['full_tag_open'] = '<div class="pagerSC">';
		$config['full_tag_close'] = '<div style="clear: left;"></div></div>';
		$config['cur_tag_open'] = '<span class="currentSC">';
		$config['cur_tag_close'] = '</span>';
		$config['prev_link'] = '&lt; Previous';
		$config['next_link'] = 'Next &gt;';
		
		$config['first_link'] = '&lt;&lt; First';
		$config['last_link'] = 'Last &gt;&gt;';
		
		$config['num_tag_open'] = '';
		$config['num_tag_close'] = '';
		$config['next_tag_open'] = '';
		$config['next_tag_close'] = '';
		$config['first_tag_close'] = '';
		$config['last_tag_open'] = '';

        $this->pagination->initialize($config);

    	
	
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['rows'] = $this->customers_model->fetch_customers($config["per_page"], $page);
        $data["links"] = $this->pagination->create_links();
		
	  // $data['rows'] = $this->customers_model->getAllCustomers();
		*/
		
		
      $meta['page_title'] = "Bank Details";
	  $data['page_title'] = "Bank Details";
      $this->load->view('common/header', $meta);
      $this->load->view('demo/bank/view_bank', $data);
      $this->load->view('common/footer');
   }
   
   function getdatatableajax()
   {
 
	   $this->load->library('datatables');
	   $this->datatables
			->select("*")
			->from("bank")
//			->join('acc_groups', 'heads.group = acc_groups.id')
		/*	->select('(SELECT SUM(warehouses_products.quantity) FROM warehouses_products WHERE warehouses_products.product_id=products.id) AS totalQuantity', FALSE)
			
			->select("warehouses_products.product_id as productid, products.image as image, products.code, products.name, products.unit, products.cost, products.price, alert_quantity,  sum(warehouses_products.quantity) as 'totalQuantity'")
			->from('warehouses_products')
			->join('products', 'warehouses_products.product_id=products.id', 'inner')
			->group_by("id")*/
			->add_column("Actions", 
			"<center>			<a href='index.php?module=heads&amp;view=edit&amp;id=$1'>
								<img src='".$this->config->base_url()."smlib/images/edit.png' width='16' height='16' title='".$this->lang->line("edit_customer")."' style='margin:1px;'/>
								</a>
							    <a href='index.php?module=heads&amp;view=delete&amp;id=$1' onClick=\"return confirm('". $this->lang->line('alert_x_customer') ."')\">
								<img src='".$this->config->base_url()."smlib/images/delete.png' width='16' height='16'' title='".$this->lang->line("delete_customer")."' style='margin:1px;'/>
								</a></center>", "id")
			->unset_column('id');
		
		
	   echo $this->datatables->generate();exit;

  }
	
	function add()
	{
		$groups = array('purchaser', 'viewer');
		if ($this->ion_auth->in_group($groups))
		{
			$this->session->set_flashdata('message', $this->lang->line("access_denied"));
			$data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
			redirect('module=home', 'refresh');
		}	
		
		//validate form input
		$this->form_validation->set_rules('name', $this->lang->line("name"), 'trim|is_unique[heads.name]|min_length[3]|max_length[50]|required|xss_clean');
		$this->form_validation->set_rules('group', $this->lang->line("group"), 'required|is_natural_no_zero|xss_clean');
	
		if ($this->form_validation->run() == true)
		{
			$name = strtolower($this->input->post('name'));
			$data = array('name' => $this->input->post('name'),
			'group' => $this->input->post('group')
			);
		}
		
		if ( $this->form_validation->run() == true && $this->heads_model->addCustomer($name,$group,$data))
		{ //check to see if we are creating the customer
			//redirect them back to the admin page
			$this->session->set_flashdata('message', $this->lang->line("head_added"));
			redirect("module=heads", 'refresh');
		}
		else
		{ //display the create customer form
			//set the flash data error message if there is one
			$data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

			$data['name'] = array('name' => 'name',
				'id' => 'name',
				'type' => 'text',
				'value' => $this->form_validation->set_value('name'),
			);
			$data['group'] = array('name' => 'group',
				'id' => 'group',
				'type' => 'select',
				'value' => $this->form_validation->set_value('group'),
			);
			
			/*$data['logo'] = array('name' => 'logo',
				'id' => 'logo',
				'type' => 'text',
				'value' => $this->form_validation->set_value('logo'),
			);
			$data['invoice_footer'] = array('name' => 'invoice_footer',
				'id' => 'invoice_footer',
				'type' => 'text',
				'value' => $this->form_validation->set_value('invoice_footer'),
			);*/
			
		$data['groups'] = $this->heads_model->getGroupList();
		$meta['page_title'] = $this->lang->line("new_heas");
		$data['page_title'] = $this->lang->line("new_head");
		$this->load->view('commons/header', $meta);
		$this->load->view('add', $data);
		$this->load->view('commons/footer');
		
		}
	}
	
	function edit($id = NULL)
	{
		if($this->input->get('id')) { $id = $this->input->get('id'); }
		$groups = array('purchaser', 'salesman', 'viewer');

		if ($this->ion_auth->in_group($groups))
		{
			$this->session->set_flashdata('message', $this->lang->line("access_denied"));
			$data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
			redirect('module=home', 'refresh');
		}
		
		$this->form_validation->set_rules('name', $this->lang->line("name"), 'required|xss_clean');
		$this->form_validation->set_rules('group', $this->lang->line("group"), 'required|is_natural_no_zero|xss_clean');
		if ($this->form_validation->run() == true)
		{
		
			$data = array('name' => $this->input->post('name'),
			'group' => $this->input->post('group') );
		}
		
		if ( $this->form_validation->run() == true && $this->heads_model->updateCustomer($id, $data))
		{ //check to see if we are updateing the customer
			//redirect them back to the admin page
			$this->session->set_flashdata('message', $this->lang->line("head_updated"));
			redirect("module=heads", 'refresh');
		}
		else
		{ //display the update form
			//set the flash data error message if there is one
			$data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

			$data['name'] = array('name' => 'name',
				'id' => 'name',
				'type' => 'text',
				'value' => $this->form_validation->set_value('name'),
			);
			$data['group'] = array('name' => 'group',
				'id' => 'group',
				'type' => 'select',
				'value' => $this->form_validation->set_value('group'),
			);
						
		
		$data['groups'] = $this->heads_model->getGroupList();			
		$data['customer'] = $this->heads_model->getCustomerByID($id);
		$meta['page_title'] = $this->lang->line("update_head");
		$data['id'] = $id;
		$data['page_title'] = $this->lang->line("update_head");
		$this->load->view('commons/header', $meta);
		$this->load->view('edit', $data);
		$this->load->view('commons/footer');
		
		}
	}
	
	function delete($id = NULL)
	{
		if($this->input->get('id')) { $id = $this->input->get('id'); }
		$groups = array('admin', 'purchaser', 'salesman', 'viewer');
		if ($this->ion_auth->in_group($groups))
		{
			$this->session->set_flashdata('message', $this->lang->line("access_denied"));
			$data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
			redirect('module=home', 'refresh');
		}
		
		if ( $this->heads_model->deleteCustomer($id) )
		{ //check to see if we are deleting the customer
			//redirect them back to the admin page
			$this->session->set_flashdata('message', $this->lang->line("head_deleted"));
			redirect("module=heads", 'refresh');
		}
		
	}
	
	
}